﻿#include "widget.h"
#include "ui_widget.h"

Widget::Widget(QWidget *parent) :
    BaseEditor(parent),
    ui(new Ui::Widget)
{
    ui->setupUi(this);

    table=ST_JOURNAL;
    listModel=new QStringListModel(this);
    ui->listView->setModel(listModel);
    ui->listView->setEditTriggers(QListView::NoEditTriggers);

    updateMonths();
    updatePublishers();
    createMenu();
}

Widget::~Widget()
{
    delete ui;
}

void Widget::updateMonths()
{
    QStringList months;
    QSqlQuery query;
    query.exec(QString("select month_ from %1 GROUP BY month_ ORDER BY month_ DESC")
               .arg(table));
    while (query.next()) {
        QString month=query.value(0).toString();
        if(month.isEmpty())
            continue;
        months.append(month);
    }

    if(!months.isEmpty())
        ui->month->addItems(months);
}

void Widget::updatePublishers()
{
    if(!publishers.isEmpty())
        publishers.clear();

    // SQL中确保name字段不能为空
    QSqlQuery query;
    query.exec(QString("select name from %1 group by name")
               .arg(table));
    while (query.next()) {
        QString publisher=query.value(0).toString();
        if(publisher.isEmpty())
            continue;
        publishers.append(publisher);
    }

    if(!publishers.isEmpty())
        ui->publisher->addItems(publishers);
}

void Widget::generateMenu()
{
    menu->addAction(QStringLiteral("查看统计信息"),
                    this,&Widget::showStatistics);
}

void Widget::showStatistics()
{
    Statistics stat(0,selectMonth,publishers);
    stat.move(x() + 100,y() + 50);
    stat.exec();
}

void Widget::updateView()
{
    if(selectPublisher.isEmpty())
        return;

    if(listModel->rowCount() != 0)
        listModel->setStringList(QStringList());

    QStringList items;
    QSqlQuery query;
    query.exec(QString("select time_ from %1 where name='%2' AND month_='%3'")
               .arg(table)
               .arg(selectPublisher)
               .arg(selectMonth));
    while (query.next()) {
        items.append(query.value(0).toString());
    }

    if(!items.isEmpty())
    {
        qSort(items);
        listModel->setStringList(items);
    }

    clean();
}

void Widget::clean()
{
    ui->evt_fin->clear();
    ui->evt_unfin->clear();
    ui->evt_fit->clear();
    ui->evt_remark->clear();
}

void Widget::on_publisher_currentTextChanged(const QString &publisher)
{
    if(selectMonth.isEmpty() || publisher.isEmpty())
        return;

    selectPublisher=publisher;
    updateView();
}

void Widget::on_rank_valueChanged(int value)
{
    switch (value) {
    case 1:
        ui->lbl_rank->setText(QStringLiteral("差"));
        break;
    case 2:
        ui->lbl_rank->setText(QStringLiteral("中"));
        break;
    case 3:
        ui->lbl_rank->setText(QStringLiteral("良"));
        break;
    case 4:
        ui->lbl_rank->setText(QStringLiteral("优"));
        break;
    default:
        ui->lbl_rank->setText(QStringLiteral("无评级"));
        break;
    }
}

void Widget::updateDetail(const QString &time)
{
    if(selectMonth.isEmpty() || selectPublisher.isEmpty())
        return;

    QSqlQuery query;
    QString sql=QString("select id,evt_fin,evt_unfin,evt_fit,evt_remark,rank from %1 where name='%2' AND month_='%3' AND time_='%4'")
            .arg(table)
            .arg(selectPublisher)
            .arg(selectMonth)
            .arg(time);
    query.exec(sql);
    if(query.next())
    {
        selectId=query.value(0).toInt();
        QString evt_fin=query.value(1).toString();
        QString evt_unfin=query.value(2).toString();
        QString evt_fit=query.value(3).toString();
        QString evt_remark=query.value(4).toString();
        bool ok;
        int rank=query.value(5).toInt(&ok);
        if(!ok)
            rank=0;

        ui->evt_fin->setPlainText(evt_fin);
        ui->evt_unfin->setPlainText(evt_unfin);
        ui->evt_fit->setPlainText(evt_fit);
        ui->evt_remark->setPlainText(evt_remark);
        ui->rank->setValue(rank);
    }
}

void Widget::on_listView_doubleClicked(const QModelIndex &index)
{
    if(index.isValid())
    {
        QString time=index.data().toString();
        if(!time.isEmpty())
            updateDetail(time);
    }
}

void Widget::on_btn_rank_clicked()
{
    int rank=ui->rank->value();
    QSqlQuery query;
    QString sql=QString("update %1 set rank=%2 where id=%3")
            .arg(table)
            .arg(rank)
            .arg(selectId);
    if(!query.exec(sql))
        ui->status->setText(QStringLiteral("异常情况，无法提交数据！"));
    else
        ui->status->clear();
}

void Widget::on_month_currentTextChanged(const QString &month)
{
    if(!month.isEmpty())
    {
        selectMonth=month;
        ui->publisher->setCurrentIndex(-1);
        listModel->setStringList(QStringList());
    }
}
